package com.example.pulltorefresh;

import java.util.Arrays;
import java.util.LinkedList;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;

import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener;

public class MainActivity extends ListActivity {    
    private LinkedList<String> mListItems;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
               
                new GetDataTask().execute();
            }
        });        

        mListItems = new LinkedList<String>();
        mListItems.addAll(Arrays.asList(mStrings));      

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplication(),
        		android.R.layout.simple_list_item_1,mListItems);

        setListAdapter(adapter);
    }

    private class GetDataTask extends AsyncTask<String, Void, String[]> {

        @Override
        protected String[] doInBackground(String... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                ;
            }
            return mStrings;
        }

        @Override
        protected void onPostExecute(String[] result) {
            mListItems.addFirst("new List Item... ");
            // Call onRefreshComplete when the list has been refreshed.
            ((PullToRefreshListView) getListView()).onRefreshComplete();
            super.onPostExecute(result);
        }
    }

    private String[] mStrings = {"USA","LONDON","BANGKOK","LAO","THAILAND",
    		"CHIANGMAI","PHITSANULOK","NONTABURI","PHRAE","UNIVERSITY"    };
}